package com.myway.service.impl;

import java.util.List;

import com.myway.dao.FlyAccessRecordsMapper;
import com.myway.dao.FlyPassengeMapper;
import com.myway.dto.common.JsonData;
import com.myway.dto.util.IDUtils;
import com.myway.pojo.FlyAccessRecords;
import com.myway.pojo.FlyPassenge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.myway.core.common.MD5Util;
import com.myway.dao.MyTabMemberInfoMapper;
import com.myway.dao.TabMemberInfoMapper;
import com.myway.pojo.TabMemberInfo;
import com.myway.service.MemberService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private TabMemberInfoMapper tabMemberInfoMapper;
    @Autowired
    private MyTabMemberInfoMapper myTabMemberInfoMapper;
    @Autowired
    private FlyPassengeMapper flyPassengeMapper;
    @Autowired
    private FlyAccessRecordsMapper flyAccessRecordsMapper;

    /**
     * 添加会员
     *
     * @param tabMemberInfo
     * @return
     */
    @Override
    public JsonData addMember(TabMemberInfo tabMemberInfo) {
        String phoneNum = tabMemberInfo.getMembPhoneNum();
        if (phoneNum != null && !"".equals(phoneNum)) {
            //去掉所有空格
            phoneNum = phoneNum.replaceAll("\\s*", "");
            tabMemberInfo.setMembPhoneNum(phoneNum);
        }
        tabMemberInfo.setMembId(IDUtils.genItemId());
        tabMemberInfo.setMembPwd(MD5Util.string2MD5(tabMemberInfo.getMembPwd()));
        tabMemberInfoMapper.insert(tabMemberInfo);
        return new JsonData(true);
    }

    /**
     * 修改会员
     *
     * @param tabMemberInfo
     * @return
     */
    @Override
    public JsonData updateMember(TabMemberInfo tabMemberInfo) {
        String phoneNum = tabMemberInfo.getMembPhoneNum();
        if (phoneNum != null && !"".equals(phoneNum)) {
            //去掉所有空格
            phoneNum = phoneNum.replaceAll("\\s*", "");
            tabMemberInfo.setMembPhoneNum(phoneNum);
        }
        tabMemberInfoMapper.updateByPrimaryKeySelective(tabMemberInfo);
        return new JsonData(true);
    }


    /**
     * 修改会员时判断会员的输入账号是否重复
     *
     * @param tabMemberInfo
     * @return
     */
    @Override
    public boolean updateIsRepetition(TabMemberInfo tabMemberInfo) {
        String phoneNum = tabMemberInfo.getMembPhoneNum();
        if (phoneNum != null && !"".equals(phoneNum)) {
            //去掉所有空格
            phoneNum = phoneNum.replaceAll("\\s*", "");
            tabMemberInfo.setMembPhoneNum(phoneNum);
        }
        List<TabMemberInfo> list = myTabMemberInfoMapper.selectMemb(tabMemberInfo);
        if (list.size() == 0) {
            return true;
        } else if (list.size() == 1) {
            String membid = list.get(0).getMembId();
            String membid2 = tabMemberInfo.getMembId();
            if (membid.equals(membid2)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 添加会员时判断会员的输入账号是否重复
     *
     * @param tabMemberInfo
     * @return
     */
    @Override
    public boolean addIsRepetition(TabMemberInfo tabMemberInfo) {
        String phoneNum = tabMemberInfo.getMembPhoneNum();
        if (phoneNum != null && !"".equals(phoneNum)) {
            //去掉所有空格
            phoneNum = phoneNum.replaceAll("\\s*", "");
            tabMemberInfo.setMembPhoneNum(phoneNum);
        }
        List<TabMemberInfo> list = myTabMemberInfoMapper.selectMemb(tabMemberInfo);
        if (list.size() == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 逻辑删除旅客常用联系人
     * @param id
     */
    @Override
    public void deleteFrequentFlyer(Long id) {
        FlyPassenge flyPassenge = flyPassengeMapper.selectByPrimaryKey(id);
        flyPassenge.setCode(null);
        flyPassengeMapper.updateByPrimaryKey(flyPassenge);
    }

    @Override
    public Boolean addAccessRecords(FlyAccessRecords flyAccessRecords){
        if(flyAccessRecords==null){
             return  false;
        }
        int accessRecordsNum = flyAccessRecordsMapper.insertSelective(flyAccessRecords);
        if(accessRecordsNum<0){
            return  false;
        }
        return true;
    }
}
